<?php
/**
 * 单页信息
 * Created by PhpStorm.
 * User: ECHO
 * Time: 9:44
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Webpageinfo extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
        //获取单页导航信息,1为单页
        $class_type = db('nav')->where(['nav_cid'=>1])->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
        $class_type = array2level($class_type,'nav');
        $this->assign('class_type', $class_type);
        // pd($class_type);
    }
    public function index(){
        $nid = input('nid');	$title = input('title');    $lang = input('lang');
        $nid?$where['a.nid']=$nid:'';
        $title?$where['a.content']=['like',"%{$title}%"]:'';
        $lang?$where['lang']=$lang:'';
        $list = db('pageinfo')->alias('a')->join('__NAV__ b','a.nid=b.id')
                ->field('a.id,a.reading,a.add_time,a.lang,b.name,b.alias')->where($where)->order('a.add_time desc')->paginate(15);
        return $this->fetch('index',['list'=>$list]);
    }
    public function add(){
        return $this->fetch('add');
    }
    public function save(){
        if($this->request->isPost()){
            $data = $this->request->except('content','post');//排除content，接收post值
            if(db('pageinfo')->where(['nid'=>$data['nid'],'lang'=>$data['lang']])->count('id') > 0) $this->error('当前栏目信息已存在');
            $data['content'] = $_POST['content'];
            if(db('pageinfo')->insert($data) !== false){
                $this->success('添加成功','index');
            }else{
                $this->error('提交失败');
            }
        }
    }
    public function edit($id){
        return $this->fetch('edit',['rows'=>db('pageinfo')->find($id)]);
    }
    public function update(){
        if($this->request->isPost()){
            $data = $this->request->except('content','post');
            if(db('pageinfo')->where(['nid'=>$data['nid'],'lang'=>$data['lang'],'id'=>['<>',$data['id']]])->count('id') > 0) $this->error('当前栏目信息已存在');
            $data['content'] = $_POST['content'];
            if(db('pageinfo')->update($data) !== false){
                $this->success('更新成功','index');
            }else{
                $this->error('更新失败');
            }
        }
    }
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if (db('pageinfo')->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的项目');
        }
    }
    //联系我们
    public function lxwm(){
        $nid = 10;
        $rows = db('pageinfo')->where(['nid'=>$nid,'lang'=>input('lang','cn')])->find();
        if($this->request->isPost()){
            $data['nid'] = $nid;	$data['content'] = json_encode($_POST['content'],JSON_UNESCAPED_UNICODE);
            $data['add_time'] = $_POST['add_time'];
            $data['lang'] = input('post.lang');
            if($rows){
                if(db('pageinfo')->where(['id'=>$rows['id']])->update($data) !==false){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            }else{
                if(db('pageinfo')->insert($data) !== false){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }
        $rows['content'] = json_decode($rows['content'],true);
        return $this->fetch('lxwm',['rows'=>$rows]);
    }
}